package com.gzhryc.weixin.gzh.models;

import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;

import com.gzhryc.weixin.api.DefaultApiResponse;

/**
 * 获取API调用凭证的响应对象
 * @author 张浩
 * @version 0.1.0
 */
public class AccessToken {

	//第三方用户唯一凭证
	private String appid;
	//第三方用户唯一凭证密钥
	private String secret;
	//API调用凭证
	private String access_token;
	//凭证有效时间，单位：秒
	private Long expires_in;
	//最后一次设置时间
	private Date lastSetDate;
	
	private AtomicBoolean running = new AtomicBoolean(false);
	
	private LinkedBlockingQueue<DefaultApiResponse> queue;
	
	public AccessToken(){}
	
	public AccessToken(String appid,String secret){
		this.appid = appid;
		this.secret = secret;
	}
	
	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getSecret() {
		return secret;
	}

	public void setSecret(String secret) {
		this.secret = secret;
	}

	public String getAccess_token() {
		return access_token;
	}
	
	public void setAccess_token(String access_token) {
		this.access_token = access_token;
		this.lastSetDate = new Date();
	}
	
	public Long getExpires_in() {
		return expires_in;
	}
	
	public void setExpires_in(Long expires_in) {
		this.expires_in = expires_in;
	}

	public Date getLastSetDate() {
		return lastSetDate;
	}

	public void setLastSetDate(Date lastSetDate) {
		this.lastSetDate = lastSetDate;
	}
	
	public boolean isRunning() {
		return this.running.get();
	}
	
	public void isRunning(boolean running) {
		this.running.set(running);
		if(running) {
			if(this.queue == null) {
				this.queue = new LinkedBlockingQueue<DefaultApiResponse>();
			}
		}else {
			this.queue.clear();
		}
	}
	
	public DefaultApiResponse queueTake() throws InterruptedException{
		if(this.queue != null) {
			return this.queue.take();
		}
		return null;
	}
	
	public void queuePut(DefaultApiResponse defApiResp) throws InterruptedException {
		if(this.queue != null) {
			this.queue.put(defApiResp);
		}
	}
}
